home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / System / NewIconsV4 / Developers / Source / ShowNI / showni.c < prev   
C/C++ Source or Header  |  1999-03-11  |  9KB  |  354 lines

  1. /* ShowNI 40.1 - By Eric Sauvageau */
  2.  
  3. /* This will take a list of icons, and display them in a window as 
  4.    buttons.  ClassAct is required.
  5.  
  6.    If the SELECT option is enabled, then clicking on an icon will output
  7.    the file's name (as passed on the commandline, so it might or might not 
  8.    have the ".info" suffix.  This is not a problem for CopyNewIcon, but
  9.    you must be aware of this if you intend to use ShowNI for other 
  10.    applications).  ShowNI will exist immediately in that case.
  11.  
  12.    Handy for icon selection in Installer scripts.
  13.    
  14.  
  15.    Usage:
  16.  
  17.    SELECT\S   = Boolean.  If present, ShowNI exits at the first selected
  18.                 icon, returning the filename.
  19.  
  20.    TITLE\K    = Optional string for the window's titlebar.  Default is 
  21.                 "ShowNI".
  22.  
  23.    LABEL\K    = Optional string for the label shown while in Select mode.
  24.                 Default is "Select an icon:".
  25.  
  26.    ICONS\M\A  = A list of icons to show in the window.  A maximum of
  27.                 15 icons are supported.
  28.  
  29. */
  30.  
  31. /*** Uncomment to enable debug output to stdout ***/
  32.  
  33. /* #define DEBUG */
  34.  
  35.  
  36.  
  37. /*** Maximum icons supported ***/
  38.  
  39. #define MAXICONS 15
  40.  
  41.  
  42. #include <clib/macros.h>
  43. #include <clib/alib_protos.h>
  44. #include <libraries/gadtools.h>
  45. #include <intuition/icclass.h>
  46. #include <intuition/intuition.h>
  47.  
  48. #include <proto/exec.h>
  49. #include <proto/dos.h>
  50. #include <proto/utility.h>
  51. #include <proto/graphics.h>
  52. #include <proto/intuition.h>
  53. #include <proto/asl.h>
  54.  
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58.  
  59. #include <classact.h>
  60. #include <classact_author.h>
  61.  
  62. #include <proto/button.h>
  63. #include <proto/window.h>
  64. #include <proto/layout.h>
  65. #include <proto/label.h>
  66. #include <proto/newicon.h>
  67.  
  68. #include <gadgets/button.h>
  69. #include <classes/window.h>
  70.  
  71. #include <gadgets/layout.h>
  72. #include <images/label.h>
  73.  
  74. #include <libraries/newicon.h>
  75.  
  76.  
  77.  
  78.  
  79. /*** GadgetIDs ***/
  80. enum
  81. {
  82.    ID_LAYOUT=1,
  83.    ID_BUTROW,
  84.    ID_WINDOW,
  85.    ID_LABEL,
  86.    ID_BUT1,
  87.  
  88.    ID_LAST
  89. };
  90.  
  91.  
  92. enum
  93. {
  94.    ARG_LABEL=0,
  95.    ARG_TITLE,
  96.    ARG_SEL,
  97.    ARG_ICONS,
  98.    ARG_LAST
  99. };
  100.  
  101.  
  102.  
  103. /* Being global, the elements are initialized to 0. */
  104.  
  105. long ARG[ARG_LAST];
  106. struct Image *imagenorm[MAXICONS];
  107. struct Image *imagesel[MAXICONS];
  108. struct NewDiskObject *diskobjects[MAXICONS];
  109.  
  110.  
  111. struct NewIconBase  *NewIconBase;
  112.  
  113.  
  114. struct NewDiskObject *MyGetNewDiskObject(UBYTE *name);
  115. VOID mystrcpy(UBYTE *to,UBYTE *from);
  116.  
  117.  
  118. int main()
  119. {
  120.     struct RDArgs *args;
  121.    BOOL selectmode;
  122.    char **filenames;
  123.    BOOL HasValidIcons=FALSE;
  124.  
  125.    int count;
  126.  
  127.    struct Window *window;
  128.    Object *Objects[ID_LAST];
  129.  
  130.    struct Screen *scr;
  131.  
  132.  
  133.     if (!(args = ReadArgs("LABEL/K,TITLE/K,S=SELECT/S,ICONS/M/A", ARG, NULL)))
  134.     {
  135.       PrintFault(IoErr(), NULL);    /* prints the appropriate error message */
  136.       return RETURN_ERROR;
  137.    }
  138.  
  139.    if (scr = LockPubScreen(NULL))
  140.    {
  141.  
  142.       NewIconBase = (struct NewIconBase *) OpenLibrary("newicon.library",39L);
  143.  
  144.       if (WindowBase && LayoutBase && ButtonBase && NewIconBase && LabelBase)
  145.       {
  146.  
  147. /*** Process passed arguments, opening icons and remapping them ***/
  148.  
  149.          selectmode = ARG[ARG_SEL];
  150. #ifdef DEBUG
  151.          if (selectmode) printf("Select mode\n");
  152. #endif
  153.  
  154.          count = 0;
  155.          filenames = (char **) ARG[ARG_ICONS];
  156.  
  157.          while ((*filenames) && (count < MAXICONS))
  158.          {
  159.  
  160.     /*** Using Nicola's version - it removes any trailing .info ***/
  161.  
  162.             if (diskobjects[count] = MyGetNewDiskObject(*filenames))
  163.             {
  164.  
  165. /* If we can get (at least one ) valid image, set HasValidIcons to TRUE. */
  166.  
  167.                if (imagenorm[count] = RemapChunkyImage(diskobjects[count]->ndo_NormalImage,scr)) (HasValidIcons = TRUE);
  168.                imagesel[count]  = RemapChunkyImage(diskobjects[count]->ndo_SelectedImage,scr);
  169.             }
  170. #ifdef DEBUG
  171.             printf("count =%d   imagenorm = %ld   imagesel = %ld   dobj = %ld\n",count, imagenorm[count], imagesel[count], diskobjects[count]);
  172.             printf("name = %s\n",(*filenames));
  173. #endif
  174.             count++;
  175.             filenames++;
  176.          };
  177.  
  178. /* Check if we have at least one valid NewIcon image to display */
  179.  
  180.          if (HasValidIcons)
  181.          {
  182.  
  183.             Objects[ID_WINDOW] = WindowObject, 
  184.                WA_Title, ((ARG[ARG_TITLE]) ? (char *)ARG[ARG_TITLE] : "ShowNI"),
  185.                WA_DepthGadget, TRUE,
  186.                WA_DragBar, TRUE,
  187.                WA_CloseGadget, TRUE,
  188.                WA_Activate,TRUE,
  189.                WINDOW_Position, WPOS_CENTERSCREEN,
  190.                WINDOW_ParentGroup, Objects[ID_LAYOUT] = VLayoutObject,
  191.                   LAYOUT_SpaceOuter, TRUE,
  192.                   LAYOUT_DeferLayout, TRUE,
  193.  
  194.                   LAYOUT_AddChild, Objects[ID_BUTROW] = HLayoutObject, HCentered,
  195.  
  196. /*** Empty group - we'll add stuff later. ***/
  197.  
  198.                   EndGroup,
  199.  
  200.                EndMember,
  201.             EndWindow;
  202.  
  203.             if (Objects[ID_WINDOW])
  204.             {
  205.  
  206. /* If select mode, then display the header label */
  207.                if (selectmode)
  208.                {
  209.                     SetAttrs(Objects[ID_LAYOUT],
  210.                        LAYOUT_AddImage, Objects[ID_LABEL] = LabelObject,
  211.                           IA_Font, scr->Font,
  212.                           LABEL_Text, ( ARG[ARG_LABEL] ? (char *)ARG[ARG_LABEL] : "Select an icon:"),
  213.                        EndImage,
  214.                   TAG_DONE);
  215.                }
  216.  
  217.                count = 0;
  218.                filenames = (char **) ARG[ARG_ICONS];
  219.  
  220.                while ((*filenames)  && (count < MAXICONS))
  221.                {
  222.  
  223. /*** Only add it if we do have an icon ***/
  224.  
  225.                   if (imagenorm[count])
  226.  
  227.                         SetAttrs(Objects[ID_BUTROW],
  228.                             LAYOUT_AddChild, ButtonObject,
  229.                                 GA_ID, (ID_BUT1+count),
  230.                                 GA_RelVerify, TRUE,
  231.                                  GA_Image, imagenorm[count],
  232.                                  GA_SelectRender, imagesel[count],
  233.                              ButtonEnd,
  234.                         CHILD_WeightedWidth,0,
  235.                         CHILD_WeightedHeight,0,
  236.                          TAG_DONE);
  237.  
  238.                      count++;
  239.                      filenames++;
  240.  
  241.                }
  242.  
  243.  
  244.                if (window = (struct Window *) CA_OpenWindow(Objects[ID_WINDOW]))
  245.  
  246. /*** We'll need it again below ***/
  247.  
  248.                filenames = (char **) ARG[ARG_ICONS];
  249.  
  250.                {
  251.                   ULONG wait, signal, result, done = FALSE;
  252.                   WORD code;
  253.  
  254.                   GetAttr(WINDOW_SigMask, Objects[ID_WINDOW], &signal);
  255.  
  256.                   while (!done)
  257.                   {
  258.                      wait = Wait(signal|SIGBREAKF_CTRL_C);
  259.  
  260.                      if (wait & SIGBREAKF_CTRL_C) done = TRUE;
  261.                      else
  262.                
  263.                      while ((result = CA_HandleInput(Objects[ID_WINDOW], &code)) != WMHI_LASTMSG)
  264.                      {
  265.                         switch (result & WMHI_CLASSMASK)
  266.                         {
  267.                            case WMHI_CLOSEWINDOW:
  268.                               done = TRUE;
  269.                               break;
  270.                      
  271.                            case WMHI_GADGETUP:
  272.                               if (selectmode)
  273.                               {
  274.                                  printf("%s\n", filenames[ (LONG) (result & WMHI_GADGETMASK) - ID_BUT1]);
  275.                                  done = TRUE;
  276.                               }
  277.                               break;
  278.  
  279.                         }
  280.                      }
  281.                   }
  282.                }
  283.  
  284.                DisposeObject(Objects[ID_WINDOW]);
  285.             }
  286.          }
  287.          else
  288.          {
  289.             printf("No valid NewIcon specified.\n");
  290.          }
  291.       }
  292.  
  293.  
  294. /*** Freeing our images and icons ***/
  295.  
  296.       count=0;
  297.  
  298. /***
  299.    Filename ptr was restored just after opening our GUI, so
  300.    no need to restore it again.
  301. ***/
  302.  
  303.       while ((*filenames)  && (count < MAXICONS))
  304.       {
  305. #ifdef DEBUG
  306.          printf("count =%d   imagenorm = %ld   imaegsel = %ld   dobj = %ld\n",count, imagenorm[count], imagesel[count], diskobjects[count]);
  307. #endif
  308.          if (imagenorm[count]) FreeRemappedImage(imagenorm[count],scr);
  309.          if (imagesel[count])  FreeRemappedImage(imagesel[count],scr);
  310.          if (diskobjects[count])   FreeNewDiskObject(diskobjects[count]);
  311.          count++;
  312.          filenames++;
  313.       }
  314.  
  315.       if (scr)  UnlockPubScreen(NULL,scr);
  316.  
  317.    }
  318.  
  319. /*** We're closing down. ***/
  320.  
  321.    if (NewIconBase) CloseLibrary((struct Library *) NewIconBase);
  322.  
  323.     FreeArgs(args);
  324.  
  325. }
  326.  
  327.  
  328.  
  329. /*** read an icon stripping trailing .info from name ***/
  330.  
  331. struct NewDiskObject *MyGetNewDiskObject(UBYTE *name)
  332. {
  333.    UBYTE buf[128];
  334.  
  335.  
  336.    mystrcpy(buf,name);
  337.    return(GetNewDiskObject(buf));
  338. }
  339.  
  340.  
  341. VOID mystrcpy(UBYTE *to,UBYTE *from)
  342. {
  343.    LONG len;
  344.  
  345.  
  346.    strcpy(to,from);
  347.    len = strlen(to);
  348.    if (len > 5 && !stricmp(&to[len-5],".info"))
  349.        to[len-5] = 0;
  350. }
  351.  
  352.  
  353. UBYTE versionstring20[] = "\0$VER: ShowNI 40.1 (8.7.97)";
  354.